/*
 * @FilePath: /data-and-structure-lv/02/binarysearch/binary-search.c
 * @Brief: 
 * @Version: 1.0
 * @Date: 2021-12-12 10:42:40
 * @Author: zsk
 * @Copyright: open source
 * @LastEditors: zsk
 * @LastEditTime: 2021-12-12 13:45:34
 */
#include <stdio.h>

typedef int ElementType;

int binary_search(ElementType *a, ElementType key, int p, int r)
{
    if (p>r)
    {
        return -1;
    }
    
    int mid = (p + r) / 2;
    if (key < a[mid])
    {
        binary_search(a, key, p, mid - 1);
    }
    else if(key>a[mid])
    {
        binary_search(a, key, mid + 1, r);
    }else if (key == a[mid])
    {
        return mid;
    }
}

void main()
{
    ElementType a[]={1,2,2,3,4,5,6,7};
    int length=8;
    ElementType key=7;
    int index=binary_search(a,key,0,length-1);
    printf("index: %d\n",index);
}